package com.atguigu.sort;

import java.util.Random;

public class QuickSort {

    private QuickSort() {
    }

    public static <E extends Comparable<E>> void Sort(E[] arr) {
        Random random = new Random();
        Sort(arr, 0, arr.length - 1, random);
    }

    private static <E extends Comparable<E>> void Sort(E[] arr, int l, int r, Random random) {
        if (l >= r) {
            return;
        }
        int p = Parition4(arr, l, r, random);
        Sort(arr, l, p - 1, random);
        Sort(arr, p + 1, r, random);
    }
    //单路快速排序
    //[l+1,j]<V   [j+1,r]>v
    private static <E extends Comparable<E>> int Parition(E[] arr, int l, int r, Random random) {
        int p = l + random.nextInt(r - l + 1);
        swap(arr, l, p);
        int j = l;
        for (int i = l + 1; i <= r; i++) {
            if (arr[i].compareTo(arr[l]) < 0) {
                j++;
                swap(arr, j, i);
            }
        }
        swap(arr, l, j);
        return j;
    }
    //双路快速排序
    private static <E extends Comparable<E>> int Parition2(E[] arr, int l, int r, Random random) {
        int p = l + random.nextInt(r - l + 1);
        swap(arr, l, p);
        int i = l + 1, j = r;
        while (true) {
            while (i <= j && arr[i].compareTo(arr[l]) < 0) {
                i++;
            }
            while (j >= i && arr[j].compareTo(arr[l]) > 0) {
                j--;
            }
            if (i >= j) break;
            swap(arr, i, j);
            i++;
            j--;
        }
        swap(arr, l, j);
        return j;
    }

    private static <E extends Comparable<E>> int Parition3(E[] arr, int l, int r, Random random) {
        int p = l + random.nextInt(r - l + 1);
        swap(arr, l, p);
        int i = l + 1, j = r;
        while (i <= j) {
            while (j >= i && arr[j].compareTo(arr[l]) >= 0) {
                j--;
            }
            while (i <= j && arr[i].compareTo(arr[l]) <= 0) {
                i++;
            }
            if (i < j) {
                swap(arr, i, j);
                i++;
                j--;
            }
        }
        swap(arr, l, j);
        return j;
    }
    private static <E extends Comparable<E>> int Parition4(E[] arr, int l, int r, Random random) {
        int p=l+random.nextInt(r-l+1);
        swap(arr,p,l);
        int i=l+1,j=r;
        while(true){
            while(i<=j&&arr[j].compareTo(arr[l])>0){
                j--;
            }
            while(i<=j&&arr[i].compareTo(arr[l])<0){
                i++;
            }
            if(i>=j)  break;
            swap(arr,i,j);
            i++;
            j--;
        }
        swap(arr,l,j);
        return j;
    }

        private static <E extends Comparable<E>> void swap(E[] arr, int j, int i) {
        E temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }

    //三路快速排序算法
    //arr[l...lt-1]<V arr[lt...gt-1]==V  arr[gt...r]>V
    public static <E extends Comparable<E>> void Sort3Ways(E[] arr) {
        Random random = new Random();
        Sort3Ways(arr, 0, arr.length - 1, random);
    }

    private static <E extends Comparable<E>> void Sort3Ways(E[] arr, int l, int r, Random random) {
        if (l >= r) {
            return;
        }
        int p = l + random.nextInt(r - l + 1);
        swap(arr, l, p);
        int lt = l, gt = r + 1, i = l + 1;
        while (i < gt) {
            if (arr[i].compareTo(arr[l]) < 0) {
                lt++;
                swap(arr, i, lt);
                i++;
            } else if (arr[i].compareTo(arr[l]) > 0) {
                gt--;
                swap(arr, i, gt);
            } else {
                i++;
            }
        }
        swap(arr, l, lt);//arr[l..lt-1]<V  arr[lt...gt-1]==V  arr[gt...r]>V
        Sort3Ways(arr, l, lt - 1, random);
        Sort3Ways(arr, gt, r, random);
    }


}
